package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entify.Category;
import com.itheima.reggie.entify.Dish;
import com.itheima.reggie.entify.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private  DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public R deleteById(Long id) {
        //检查该分类下是否关联其他菜品，如果关联了，则不能删除
        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.eq(Dish::getCategoryId,id);
        Integer count = dishMapper.selectCount(dishlqw);
        if(count>0){
            return R.error("当前分类下有菜品,不能删除");
        }
        //检查该分类下是否关联套餐，如果关联了，则不能删除
        LambdaQueryWrapper<Setmeal> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.eq(Setmeal::getCategoryId,id);
        Integer count1 = setmealMapper.selectCount(setmeallqw);
        if(count1>0){
            return R.error("当前分类下有套餐,不能删除");
        }
        //删除分类
        int deleteById = categoryMapper.deleteById(id);
        return deleteById>0?R.success("分类删除成功"):R.error("分类删除失败");
    }
}
